﻿--[[
   @brief    ui logic of game_update
   @author   saintzhao
   @version  1.0
   @date     2015-04-03
--]]

game_update_control = {
    init = function() 
      ierd_tgp.tpf_ui.Require("tool_box\\model.lua");
      event_center.add_listener(tool_box_event.TOOL_BOX_ON_JS_CALLBACK_GU, 'game_update_control.on_js_callback')
    end;

    update_game_base_info_list = function()
        ierd_tgp.tpf_ui.Log("[game_update_control]update_game_base_info_list");       
        local game_count = table.getn(tool_box_model.my_game_id_list);
        local temp_game_list = banner_model.game_base_info_list;             
        local list_count = temp_game_list.count;
        tool_box_model.my_game_list_info = {};
        for i = 1, game_count do 
            for j = 1, list_count do
                if tool_box_model.my_game_id_list[i] == temp_game_list.game_base_info[j - 1].id then
                    table.insert(tool_box_model.my_game_list_info, {
                        id = temp_game_list.game_base_info[j - 1].id,
                        name = temp_game_list.game_base_info[j - 1].name,
                        e_name = temp_game_list.game_base_info[j - 1].e_name,
                        icon_url = temp_game_list.game_base_info[j - 1].banner_icon_url,
                        type = temp_game_list.game_base_info[j - 1].type,
                    });
                end;
            end;
        end;

        local json_list_info = base_utility.table_to_json(tool_box_model.my_game_list_info);
        web_utility.lua_call_js('gu_send_game_update_list', json_list_info, CID_TOOLBOX_QBWEB);          
    end;

    is_showed_in_banner_list = function(game_id)
        return banner_model.is_showed_in_banner_list(game_id);
    end;

    is_installed_in_local = function(game_id)
        if game_id == nil then
            return false;
        end

        local game_lib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        local installed_game_list = ierd_tgp.game_library.Game_list:new_local();
        if game_lib ~= nil then
            game_lib:get_installed_games(installed_game_list);
        else
            return false;
        end;

        local is_installed = false;
        local game_base_info = banner_model.get_game_base_info(game_id);
        if game_base_info ~= nil and
           game_base_info.main_type ~= nil then
            if game_base_info.main_type ~= tool_box_model.game_main_type.WEB_GAME and 
               game_base_info.main_type ~= tool_box_model.game_main_type.MOBILE_GAME then
                for j = 0, installed_game_list.count - 1  do
                    if installed_game_list.game_id_list[j] ~= nil and 
                       installed_game_list.game_id_list[j] == game_id then
                        is_installed = true;
                        break;
                    end
                end
             else
                is_installed = true;
             end
         end
        return is_installed;
    end;
    
    get_my_game_id_list = function()
        -- 需要在banner列表中,并且如果不是页游或手游，需要本地已安装
        local game_lib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        local installed_game_list = ierd_tgp.game_library.Game_list:new_local();
        if game_lib ~= nil then
            game_lib:get_installed_games(installed_game_list);
        else
            return false;
        end;
        
        local filter_count = 0;
        tool_box_model.my_game_id_list = {n = filter_count};

        for i = 0, installed_game_list.count - 1 do
            local is_in_visible_list = false;

            local game_id = installed_game_list.game_id_list[i];

            is_in_visible_list = game_update_control.is_showed_in_banner_list(tonumber(game_id));

            base_utility.log("[game_update_control][get_my_game_id_list]game " .. tostring(game_id) ..
                ", visible:" .. tostring(is_in_visible_list));

            if is_in_visible_list == true then
                filter_count = filter_count + 1;
                tool_box_model.my_game_id_list[filter_count] = game_id;
            end
        end;     
        tool_box_model.my_game_id_list.n = filter_count;
    end;
    
    sort_my_game_id_list = function(state_list, count)
        local need_update_list = {};
        local pre_download_list = {};
        local pre_download_list_ex = {};
        local normal_list = {};
        for i = 1, table.getn(tool_box_model.my_game_id_list) do
            for j = 1, count do
                if tool_box_model.my_game_id_list[i] == state_list.game_state[j - 1].iGame_id then
                    if state_list.game_state[j - 1].iState == 4 or
                       state_list.game_state[j - 1].iState == 5 or
                       state_list.game_state[j - 1].iState == 6 then
                       --游戏需要更新、正在更新和游戏更新失败的列表
                        table.insert(need_update_list, tool_box_model.my_game_id_list[i]);
                    elseif state_list.game_state[j - 1].iPre_download == 1 then
                        local game_lib =
                            base_utility.get_component_adapter("IGame_library",
                                "ierd_tgp.game_library.Get_game_library_adapter();");
                        local state_info = ierd_tgp.game_library.Pre_Update_Status_Info:new();
                            if game_lib ~= nil then
                                game_lib:get_preupdate_status_info(state_list.game_state[j - 1].iGame_id, 
                                state_info);
                            end;
                        if state_info.formal_update_after_ > 0 then 
                            table.insert(pre_download_list, tool_box_model.my_game_id_list[i]);
                        else
                            table.insert(normal_list, tool_box_model.my_game_id_list[i]);
                        end;
                        state_info:delete();
                    --elseif state_list.game_state[j - 1].iState == 5 then
                    --    table.insert(updating_list, tool_box_model.my_game_id_list[i]);
                    else
                        table.insert(normal_list, tool_box_model.my_game_id_list[i]);
                    end;
                end;
            end;
        end;
        tool_box_model.my_game_id_list = {};
        for i = 1, table.getn(need_update_list) do
            table.insert(tool_box_model.my_game_id_list, need_update_list[i]);
        end;
        for i = 1, table.getn(pre_download_list) do
            table.insert(tool_box_model.my_game_id_list, pre_download_list[i]);
        end;
        --for i = 1, table.getn(pre_download_list_ex) do
        --    table.insert(tool_box_model.my_game_id_list, pre_download_list_ex[i]);
        --end;
        for i = 1, table.getn(normal_list) do
            table.insert(tool_box_model.my_game_id_list, normal_list[i]);
        end;
    end;
    
    update_game_state_list = function(event_id)
        ierd_tgp.tpf_ui.Log("[game_update_control]update_game_state_list");
        if tool_box_model.game_update_page_opend == true then 
            local list_info = {};
            local temp_list = banner_model.game_state_list;
            local list_count = temp_list.count; 
            list_info.list = {};
            local pre_download_state = {};
            local length = 0;
            for i = 1, table.getn(tool_box_model.my_game_id_list) do
                for j = 1, list_count do
                    if tool_box_model.my_game_id_list[i] == temp_list.game_state[j - 1].iGame_id then
                        if temp_list.game_state[j - 1].iPre_download == 1 then
                            length = length + 1;
                        end;
                    end;
                end;
            end;
            if tool_box_model.pre_update_info_length == 0 or
               tool_box_model.pre_update_info_length ~= length or
               event_id == 0 then
                game_update_control.get_my_game_id_list();
                game_update_control.sort_my_game_id_list(temp_list, list_count);
                game_update_control.update_game_base_info_list();
                tool_box_model.pre_update_info_length = length;
            end;
            for i = 1, table.getn(tool_box_model.my_game_id_list) do
                for j = 1, list_count do
                    if tool_box_model.my_game_id_list[i] == temp_list.game_state[j - 1].iGame_id then
                        if temp_list.game_state[j - 1].iPre_download == 1 then
                            local game_lib =
                                base_utility.get_component_adapter("IGame_library",
                                    "ierd_tgp.game_library.Get_game_library_adapter();");
                            local state_info = ierd_tgp.game_library.Pre_Update_Status_Info:new();
                            if game_lib ~= nil then
                                game_lib:get_preupdate_status_info(temp_list.game_state[j - 1].iGame_id, 
                                state_info);
                            end;
                            table.insert(list_info.list,{
                                id = temp_list.game_state[j - 1].iGame_id,
                                name =  tool_box_model.my_game_list_info[i].name,
                                e_name =  tool_box_model.my_game_list_info[i].e_name,
                                icon_url =  tool_box_model.my_game_list_info[i].icon_url,
                                type =  tool_box_model.my_game_list_info[i].type,
                                state = temp_list.game_state[j - 1].iState,
                                support_update = temp_list.game_state[j - 1].iSupport_update,
                                progress = temp_list.game_state[j - 1].iProgress,
                                auto_update = temp_list.game_state[j - 1].iAuto_update,
                                download_mode = temp_list.game_state[j - 1].download_mode, -- 0:未知，1:普通下载，2:低速下载
                                pre_download = temp_list.game_state[j - 1].iPre_download,
                                pre_download_status = temp_list.game_state[j - 1].pre_download_status,
                                pre_download_state = {
                                    game_id = temp_list.game_state[j - 1].iGame_id,
                                    remian_times = state_info.formal_update_after_;
                                    game_version = state_info.version,
                                    phase = state_info.phase,
                                    pack_size = state_info.pkg_size_,
                                    announce_content = (string.gsub(state_info.content, '\n' , '\\\\')),
                                    pre_download_progress = state_info.download_progress_,
                                    current_status_ = state_info.current_status_,
                                    pkg_path = (string.gsub(state_info.pkg_path, '\\' , '\\\\')),
                                    versionlist_url = state_info.versionlist_url_                                  
                                }
                            });
                            state_info:delete();
                        else
                            table.insert(list_info.list,{
                                id = temp_list.game_state[j - 1].iGame_id,
                                name =  tool_box_model.my_game_list_info[i].name,
                                e_name =  tool_box_model.my_game_list_info[i].e_name,
                                icon_url =  tool_box_model.my_game_list_info[i].icon_url,
                                type =  tool_box_model.my_game_list_info[i].type,
                                state = temp_list.game_state[j - 1].iState,
                                support_update = temp_list.game_state[j - 1].iSupport_update,
                                progress = temp_list.game_state[j - 1].iProgress,
                                auto_update = temp_list.game_state[j - 1].iAuto_update,
                                download_mode = temp_list.game_state[j - 1].download_mode,
                                pre_download = temp_list.game_state[j - 1].iPre_download,
                                pre_download_status = temp_list.game_state[j - 1].pre_download_status,
                                pre_download_state = {}
                            });
                        end;                        
                    end;
                end;
            end;
            local json_list_info = base_utility.table_to_json(list_info);
            ierd_tgp.tpf_ui.Log("[game_update_control]json_list_info="..json_list_info);
            web_utility.lua_call_js('gu_send_game_state_list', json_list_info, CID_TOOLBOX_QBWEB);
        end;       
    end;

    is_game_router = function()
        local result;
        ierd_tgp.tpf_ui.Log("[game_update_control]router_vendor="..tool_box_model.route_info.vendor);
        if 'xiaomi' == tool_box_model.route_info.vendor then
            result = 1;
        elseif 'huawei' == tool_box_model.route_info.vendor then
            result = 2;
        else
            result = 0;
        end;

        local str = '{"result":' .. tostring(result) .. '}';
        web_utility.lua_call_js('gu_send_route_info_vendor', str, CID_TOOLBOX_QBWEB);
        web_utility.lua_call_js('gu_send_route_vendor', str, CID_MAINFRAME_QBWEB);
    end;
    
    open_preupdate_pkg_folder = function(param)
        local game_lib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        if game_lib ~= nil then
            if param.pkg_path ~= "" then
                game_lib:open_game_path(0, param.path);
            else 
                game_lib:open_game_path(param.game_id, param.path);
            end;
        end;
    end;

    change_auto_update_setting = function(param)
        local game_lib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        if game_lib ~= nil then
            game_lib:set_auto_update_game(param.game_id, param.auto_update);
        end;
    end;
    
    start_update_game = function(game_id)
        local data_out = comm_center.call_service("i_service_allow_continue_operation_game",
                            {game_id = game_id});
        if data_out ~= nil and not data_out.bAllowContinue then
            return
        end
        
        local game_lib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        if game_lib ~= nil then
            local server_id = game_lib:get_last_selected_server(game_id);
            local server = banner_model.get_server_by_zoneid(game_id, server_id);
            if server ~= nil and server.pre_update == true then
                local json_tbl = {};
                json_tbl.game_id_ = game_id;
                json_tbl.zone_id_ = server_id;
                local json_str = base_utility.table_to_json(json_tbl);
                event_center.send_event(popup_wnd_event.PRE_UPDATE_GAME, json_str);
            else
                game_lib:update_game(game_id, server_id, ierd_tgp.game_library.LAUNCH_OP_FROM_TOOLBOX_GAMEUPDATE);
                event_center.send_event(main_event.UPDATE_GAME_START, "");
            end;
        end;
    end;
    
    start_preupdate_game = function(game_id)
        local game_lib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        if game_lib ~= nil then
            local data_out = comm_center.call_service(
                "i_service_allow_continue_preupdate_game",
                {game_id = game_id});
            if data_out ~= nil and not data_out.bAllowContinue then
                return;
            end
            game_lib:start_preupdate_game(game_id, 1);
            state_operation_panel.set_preupdate_mutex(
                game_id, true); 
        end;
    end;
    
    pause_preupdate_game = function(game_id)
        local game_lib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        if game_lib ~= nil then
            game_lib:pause_preupdate_game(game_id, ierd_tgp.game_library.OperationSource.kPauseByUser, false,
                                 ierd_tgp.game_library.LAUNCH_OP_FROM_TOOLBOX_GAMEUPDATE,
                                 CID_TOOLBOX_DLG);
            state_operation_panel.set_preupdate_mutex(game_id, false);   
        end;
    end;    
    
    on_js_callback = function (event_id, jsonp)
        tool_box_model.game_update_page_opend = true;
        local data_table = base_utility.json_to_table(jsonp);
        local fuc_name = data_table["fuc_name"];
        local str;
        if data_table["json"] ~= "" then
            str = base_utility.json_to_table(data_table["json"]);
        end;
        if fuc_name == "gu_get_game_update_list" then
            game_update_control.update_game_base_info_list();
        elseif fuc_name == "gu_get_game_state_list" then
            game_update_control.update_game_state_list(0);
        elseif fuc_name == "gu_open_game_path" then
            game_update_control.open_preupdate_pkg_folder(str);
        elseif fuc_name == "gu_start_update_game" then
            game_update_control.start_update_game(str["id"]); 
        elseif fuc_name == "gu_start_preupdate_game" then
            game_update_control.start_preupdate_game(str["id"]);
        elseif fuc_name == "gu_pause_preupdate_game" then
            game_update_control.pause_preupdate_game(str["id"]); 
        elseif fuc_name == "gu_open_update_speed_setting" then
            event_center.send_event(main_menue_event.MAIN_MENUE_SETTING, base_utility.table_to_json(str));
        elseif fuc_name == "gu_open_game_library" then
            web_utility.open_link(base_utility.table_to_json(str));
        elseif fuc_name == "gu_change_auto_update_setting" then
            game_update_control.change_auto_update_setting(str);
        elseif fuc_name == "gu_get_route_info_vendor" then
            game_update_control.is_game_router();
        end;
    end; 

    start_preupdate_game_ex = function(data_in)
        local param_data = comm_center.call_service("Svr_IsOpenGameDownloadMgr", nil);
        if param_data == nil then
            ierd_tgp.tpf_ui.Log("[GameDownloadMgr][start_preupdate_game_ex]param_data is nil");
            return;
        end;
        if param_data.is_open == false then
            ierd_tgp.tpf_ui.Log("[GameDownloadMgr][start_preupdate_game_ex]switch is closed");
            return;
        end;

        if data_in == nil or data_in.game_id == nil then
            ierd_tgp.tpf_ui.Log("[GameDownloadMgr][start_preupdate_game_ex] data is nil");
            return;
        end
        local game_lib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        if game_lib ~= nil then
            local data_out = comm_center.call_service(
                "i_service_allow_continue_preupdate_game",
                {game_id = data_in.game_id});
            if data_out ~= nil and not data_out.bAllowContinue then
                return;
            end
            game_lib:start_preupdate_game(data_in.game_id, 7);
            state_operation_panel.set_preupdate_mutex(
                data_in.game_id, true); 
        end;
    end;
};
game_update_control.init();